package com.example.connecting_dots_android;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

public class PlayeresNameAndSignPick extends Activity {

	private EditText mPlayerOne, mPlayerTwo;
	private CheckBox mChechBoxPlayWithComputer;
	private boolean mIsMute = false;
	private Button mBtnStartGame;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_players_selection);
		init();
		settings();
	}

	/**
	 * method to manage the background image and the music
	 * */
	private void settings() {
		
		if (MainActivity.mServ!= null && mIsMute && !MainActivity.mServ.isPlaying()) {
			MainActivity.mServ.resume();
		}
		// Show the Up button in the action bar.
		setupActionBar();
		
		Utils.setBackgroundImage(this, findViewById(R.id.playerSignPickLayout));
	}

	/**
	 * method to initialize class members
	 * */
	private void init() {
		
		mIsMute = Utils.getIsMute(this);
		
		mPlayerOne = (EditText) findViewById(R.id.idNameFirstPlayer);
		mPlayerTwo = (EditText) findViewById(R.id.idNameSecondPlayer);
		mBtnStartGame = (Button) findViewById(R.id.idStartGameBtn);
		mChechBoxPlayWithComputer = (CheckBox) findViewById(R.id.checkBoxPlayWithComputer);
		
		mPlayerOne.addTextChangedListener(mTextWatcher);
		mPlayerTwo.addTextChangedListener(mTextWatcher);
		
		mBtnStartGame.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						startGame(v);
					}
		});
		
		mChechBoxPlayWithComputer.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

		       @Override
		       public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
		    	   if(isChecked){
		    		   mPlayerTwo.setText(Constants.COMPUTER_PLAYER);
		    		   mPlayerTwo.setEnabled(false);
		    	   }else{
		    		   mPlayerTwo.setText("");
		    		   mPlayerTwo.setEnabled(true);
		    	   }
		       }
		   }
		); 
		
		// run once to disable if empty
		checkFieldsForEmptyValues();
	}

	/**
	 * method to save entered players' names and go to StartGame activity
	 * */
	public void startGame(View view) {
		Intent intent = new Intent(this, StartGame.class);
		String playerOneNameToString = mPlayerOne.getText().toString();
		String playerTwoNameToString;
		if(mChechBoxPlayWithComputer.isChecked()){
			playerTwoNameToString = Constants.COMPUTER_PLAYER;
		}else{
			playerTwoNameToString = mPlayerTwo.getText().toString();
		}

		intent.putExtra("playerOneName", playerOneNameToString);
		intent.putExtra("playerTwoName", playerTwoNameToString);
		startActivity(intent);
	}

	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.playeres_name_and_sign_pick, menu);
		checkFieldsForEmptyValues();
		return true;
	}

	/**
	 * disables start button until the required number of players are entered
	 * */
	private TextWatcher mTextWatcher = new TextWatcher() {
		@Override
		public void beforeTextChanged(CharSequence charSequence, int i, int i2,
				int i3) {
		}

		@Override
		public void onTextChanged(CharSequence charSequence, int i, int i2,
				int i3) {
		}

		@Override
		public void afterTextChanged(Editable editable) {
			// check Fields For Empty Values
			checkFieldsForEmptyValues();
		}
	};

	/**
	 * method to check EditText fields values
	 * */
	private void checkFieldsForEmptyValues() {

		String s1 = mPlayerOne.getText().toString();
		String s2 = mPlayerTwo.getText().toString();

		if (s1.equals("") || s2.equals("")) {
			mBtnStartGame.setEnabled(false);
		} else {
			mBtnStartGame.setEnabled(true);
		}
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		
		Utils.setBackgroundImage(this, findViewById(R.id.playerSignPickLayout));
		
		mIsMute = Utils.getIsMute(this);
		if (MainActivity.mServ!= null && mIsMute && !MainActivity.mServ.isPlaying()) {
			MainActivity.mServ.resume();
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		
		mIsMute = Utils.getIsMute(this);
		if (MainActivity.mServ!= null && mIsMute) {
			MainActivity.mServ.pause();
		}
	}
	
}
